<?php


   class Message_model Extends Model
   {
      function message_model($msg_id = null)
      {
	  parent::Model();


	$this->msg_tbl 			= 'message';
	$this->msg_track_tbl 		= 'msg_track';
	$this->msg_view_tbl 		= 'msg_viewer';
	
      }
      
	function getSingleMessage($msg_id)
	{
		//Updated
		$this->db->where('MSG_ID', $msg_id);
		$query = $this->db->get($this->msg_tbl, 1);

		if ( $query->num_rows() == 1 )
			{
				$message = $query->row_array();
				
				return $message;
			}
			else
			{
				return false;
			}
	}

      function getSingleUserMessage($user_id, $msg_id)
      {
	      //Updated
	        $this->db->where('author_id', $user_id);
		$this->db->where('MSG_ID', $msg_id);
		$query = $this->db->get($this->msg_tbl, 1);

		if ( $query->num_rows() == 1 )
			{
				$message = $query->row_array();
				
				return $message;
			}
			else
			{
				return false;
			}
	
      }
      
      function getMessagesBeforeDate($uid = null, $lastDate = null)
      {
	      //Updated
         
         //Evaluate lastdate if it is empty mktime else just use current lastdate
         $lastDate = empty($lastDate) ? mktime() : $lastDate;
         

	 $this->db->where('author_id', $user_id);
	 $this->db->where('MSG_DATE <=', $lastDate);
	 $this->db->orderby('MSG_TYPE' , 'desc');
	 $this->db->orderby('MSG_DATE' , 'desc');

	$query = $this->db->get($this->msg_tbl, 1);

	$built = array();

		if ( $query->num_rows()  <= 0  )
			{
				return NULL;
			}

		$messages = $query->result_array();

		foreach ($messages as $message)
		{
			if (!empty($message['msg_id']))
				{
						
				}
					
			$built[] = $message;
		}
			
		return $built;
                   	
      }
      
      function updateTrack($user_id, $msg_id)
      {
	      //Updated
	      $this->db->where('msg_id', $msg_id);

	      		if ($this->db->update('msg_track', $msgTrackData)) {
			return $msg_id;
		} else {
			return FALSE;
		}
	      $currentTS = mktime();
	      $data = array('USER_ID'=> $user_id, 'MSG_ID' => $msg_id, 'READ_TS' => $currentTS);

	 	$query = $this->db->insert($this->msg_track_tbl, $data);
        	
	      return ($query == DB_OK) ? true : false;	
      }
      
      function getAllMessages()
      {
	      //Updated
         

	$this->db->orderby('MSG_TITLE' , 'desc');

	$query = $this->db->get($this->msg_tbl, 1);

	$built = array();

		if ( $query->num_rows()  <= 0  )
			{
				return NULL;
			}

		$messages = $query->result_array();

		foreach ($messages as $message)
		{
			if (!empty($message['msg_id']))
				{
						
				}
					
			$built[] = $message;
		}
			
		return $built;

      }


      function getAllUserMessages($user_id)
      {
	      //Updated
         
	$this->db->where('author_id', $user_id);
	$this->db->orderby('MSG_TITLE' , 'desc');

	$query = $this->db->get($this->msg_tbl, 1);

	$retArr = array();

		if ( $query->num_rows()  <= 0  )
			{
				return NULL;
			}

		$messages = $query->result_array();

		foreach ($messages as $message)
		{
			if (!empty($message['msg_id']))
				{
						
				}
					
			$built[] = $message;
		}
			
		return $built;

      }    

          function getUserMessageBrief($user_id, $num)
	  {
		  //Updated
		  
		  $this->db->select('MSG_DATE , MSG_TITLE'); 
		  $this->db->where('AUTHOR_ID', $user_id);
		$this->db->orderby('MSG_DATE' , 'desc');

		$query = $this->db->get($this->msg_tbl, 1);

		$retArr = array();

		if ( $query->num_rows()  <= 0  )
			{
				return NULL;
			}

		$messages = $query->result_array();

		foreach ($messages as $message)
		{
			if (!empty($message['msg_id']))
				{
						
				}
					
			$built[] = $message;
		}
			
		return $built;

      }    

      function saveMessage($message)
      {
	      //Updated
	      //$message is an array that will need to be passed from the message controller
	if ( !empty($message['msg_id']) )
	{
		$this->db->where('msg_id', $message['msg_id']);
				
		unset($message['msg_id']);
				
		return $this->db->update($this->msg_tbl, $message);
	} else {
		$message['msg_date'] = time();
				
		$query = $this->db->insert($this->msg_tbl, $message);
					
		return $this->db->insert_id();
	}
         
      }
      
      function deleteMessage($msg_id)
      {
	      //Updated
	 $this->db->where('msg_id', $msg_id);
	$this->db->delete($this->msg_tbl);
      }
      
      /*function modifyMessage($mid, $title, $date, $msg, $flag)
      {
	      //Not Updated
         $title = $this->dbi->quote(addslashes($title));
         $msg = $this->dbi->quote(addslashes($msg));
         $stmt = "UPDATE $this->msg_tbl SET MSG_TITLE = $title, MSG_CONTENTS = $msg, MSG_DATE = $date WHERE MSG_ID = $this->msg_id";
         //echo $stmt;
         $result = $this->dbi->query($stmt);
         return ($result == DB_OK) ? true : false;
         
      }*/
      
      function isRead($mid = null)
      {
	      //Not Updated
         $this->setMessageID($mid);
         $stmt = "SELECT MSG_ID FROM $this->msg_track_tbl WHERE MSG_ID = $this->msg_id";	
         $result = $this->dbi->query($stmt);         
         return $result->numRows();
      }
      
      function getViewers($mid = null)
      {
         //Not Updated
         $this->setMessageID($mid);
         $stmt = "SELECT VIEWER_ID FROM $this->msg_view_tbl WHERE MSG_ID = $this->msg_id";	
         
         $result = $this->dbi->query($stmt);
         if ($result->numRows() < 0)
         {
            return null;	
         }
         $retArr = array();
         while ($row = $result->fetchRow())
         {
            $retArr[] = $row->VIEWER_ID;
         }
         return $retArr;
      }
      
      function addViewer($mid, $views)
      {
	      //Not Updated
         $this->setMessageID($mid);
         
         foreach($views as $view_id)
         {
           $stmt = "INSERT INTO $this->msg_view_tbl(MSG_ID, VIEWER_ID) VALUES($this->msg_id, $view_id)";           
           //echo $stmt;
           $result = $this->dbi->query($stmt);	
         }
 	
      }
      
      function deleteViewers($mid = null)
      {
	      //Not Updated
      	$this->setMessageID($mid);
        $stmt = "DELETE FROM $this->msg_view_tbl WHERE MSG_ID = $this->msg_id";
        $result = $this->dbi->query($stmt);
      }
      
      function isViewable($mid = null, $vid)
      {
	      //Not Updated
        $this->setMessageID($mid);
        $stmt = "SELECT VIEWER_ID FROM $this->msg_view_tbl WHERE MSG_ID = $this->msg_id AND VIEWER_ID = $vid OR MSG_ID = $this->msg_id AND VIEWER_ID = 0";
        //echo $stmt;
        $result = $this->dbi->query($stmt);
        return $result->numRows();
      }
      
      function getMsgIDbyMessageTitle($title)
      {
	      //Not Updated
        $title = $this->dbi->quote(addslashes($title));
        $stmt = "SELECT MSG_ID FROM $this->msg_tbl WHERE MSG_TITLE = $title";
        //echo $stmt;
        $result = $this->dbi->query($stmt);
        if ($result->numRows() <= 0)
        {
           return null;	
        }
        $row = $result->fetchRow();
        return $row->MSG_ID;
      }

   }


?>
